﻿using Dt261.Model;
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dt261.db
{
    class MemberInfoDb
    {
        private readonly SQLiteConnection connection;

        public MemberInfoDb(string dbPath)
        {
            connection = new SQLiteConnection(dbPath);
            connection.CreateTable<MemberInfo>();
        }

        public int AddMember(MemberInfo m)
        {
            if (string.IsNullOrWhiteSpace(m.Name))
            {
                throw new Exception(" name are required!");
                //return 0;
            }
            // 
            return connection.Insert(m);
            //return connection.Insert(new MemberInfo()
            //{

            //    Address = m.Address,
            //    Age = m.Age,
            //    AllergyHistory = m.AllergyHistory,
            //    Birthday = m.Birthday,
            //    CreateTime = DateTime.Now,
            //    DeafAidLeftModel = m.DeafAidLeftModel,
            //    DeafAidRightModel = m.DeafAidRightModel,
            //    DeafAidRoutine = m.DeafAidRoutine,
            //    DeafAidThreshold = m.DeafAidThreshold,
            //    DeafAidVolume = m.DeafAidVolume,
            //    DeviceWT = m.DeviceWT,
            //    Email = m.Email,
            //    Gender = m.Gender,
            //    History = m.History,
            //    HospitalId = m.HospitalId,
            //    Uid = m.Uid,
            //    IdCard = m.IdCard,
            //    Insurance = m.Insurance,
            //    IsUpload = m.IsUpload,
            //    LastTestTime = m.LastTestTime,
            //    ListenWord = m.ListenWord,
            //    Mark = m.Mark,
            //    Name = m.Name,
            //    Note = m.Note,
            //    Phone = m.Phone,
            //    SoundField = m.SoundField,
            //    Strength = m.Strength,
            //    TestTime = m.TestTime,
            //    Tinnitus = m.Tinnitus,
            //    ZipCode = m.ZipCode,
            //    State = m.State,
            //    ModifyDate = m.ModifyDate,
            //    UserId = m.UserId,

            //});
        }

        public int UpdateMember(MemberInfo m)
        {
            if (string.IsNullOrWhiteSpace(m.Name))
                throw new Exception(" name  are required!");

            IList<MemberInfo> list = connection.Query<MemberInfo>("select * from memberinfo where id = ?", m.Id);

            if (list.Count() > 0)
            {
                return connection.Update(m);
            }
            else
            {
                return this.AddMember(m);
            }
        }

        public IList<MemberInfo> GetMembers()
            => connection.Table<MemberInfo>().ToList();

        public IList<MemberInfo> GetMemberByUid(string uid)
        {
            IList<MemberInfo> list = connection.Query<MemberInfo>("select * from memberinfo where uid = ?", uid);

            return list;
        }

        public IList<MemberInfo> GetMemberById(int id)
        {
            IList<MemberInfo> list = connection.Query<MemberInfo>("select * from memberinfo where id = ?", id);

            return list;
        }

        public IList<MemberInfo> GetMembersByUserId(string userid)
        {
            IList<MemberInfo> list = connection.Query<MemberInfo>("select * from memberinfo where UserId = ?  order by createTime DESC", userid);

            return list;
        }

        public MemberInfo GetOneMemberByUid(string uid)
        {
            IList<MemberInfo> list = GetMemberByUid(uid);

            return list.Count() > 0 ? list[0] : null;
        }

        public MemberInfo GetOneMemberById(int id)
        {
            IList<MemberInfo> list = GetMemberById(id);

            return list.Count() > 0 ? list[0] : null;
        }

        public int DeleteMemberInfo(MemberInfo m)
        {
            if (m != null)
                return connection.Delete(m);
            return 0;
        }

        public IList<MemberInfo> SearchMemberInfo(string userid, string key)
        {
            if (key != null)
            {
                if (key == "ALL")
                {
                    IList<MemberInfo> list = connection.Query<MemberInfo>("select * from memberinfo where name like ?  order by createTime DESC", "%" + "%");
                    return list;
                }
                else
                {
                    string keys = "%" + key + "%";
                    IList<MemberInfo> list = connection.Query<MemberInfo>("select * from memberinfo where name like ? or  Insurance LIKE ? or Phone LIKE ? and  UserId = ?  order by createTime DESC", keys,keys,keys, userid);
                    return list;
                }

            }

            return null;
        }
    }
}
